#include <iostream>
#include <fstream>
#include <cstdio>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <deque>
#include <ctime>
#include <cstdlib>

using namespace std;

#define sz(x) ((int)((x).size()))

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const double EPS = 1e-9;
const double PI = acos(-1.0);

const int MAXN = 2050;

int n;
vector<int> b[MAXN];
int mt[2050], rmt[2050];

int main() {
	//freopen(".in", "r", stdin);
	//freopen(".out", "w", stdout);

	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		int k;
		scanf("%d", &k);
		while (k-- > 0) {
			int to;
			scanf("%d", &to);
			--to;
			b[i].push_back(to);
		}
		sort(b[i].begin(), b[i].end());
	}
	for (int i = 0; i < n; i++) {
		int x;
		scanf("%d", &x);
		--x;
		mt[i] = x;
		rmt[x] = i;
	}
	for (int i = 0; i < n; i++) {
		vector<int> ans;
		for (int j = 0; j < sz(b[i]); j++) {
			if (mt[i] == b[i][j]) {
				ans.push_back(b[i][j]);
			} else {
				int v = rmt[b[i][j]];
				if (binary_search(b[v].begin(), b[v].end(), mt[i])) {
					ans.push_back(b[i][j]);
				}
			}
		}
		printf("%d", sz(ans));
		for (int i = 0; i < sz(ans); i++) {
			printf(" %d", ans[i] + 1);
		}
		puts("");
	}

	return 0;
}
